package app.auto.runner.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.Base64;
import android.util.TypedValue;
import android.view.ViewGroup;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;

/***
 * dip px 互转
 * @author Administrator
 *
 */
public class DipUtil {

    public static float scale;
    public static Context context;
    public static float origScale;

    public static float getDensity(Context context) {
        if (scale == 0) {
            scale = context.getResources().getDisplayMetrics().density;//density值表示每英寸有多少个显示点
        }
        if (origScale == 0) {
            origScale = context.getResources().getDisplayMetrics().density;
        }
        return scale;
    }

    /**
     * 像素转换密度
     */
    public static float px2dip(float pxValue) {
        final float scale = getDensity();
        return new Float(pxValue / scale).floatValue();
    }

    public static float getScale(Activity context) {
        return context.getResources().getDisplayMetrics().scaledDensity;
    }

    /**
     * 密度转换像素
     */
    public static int fromDip(Context context, float dipValue) {
        return (int) (dipValue * getDensity(context));
    }

    public static float getDensity() {
        return getDensity(context);
    }

    public static float getScale() {
        return context.getResources().getDisplayMetrics().scaledDensity;
    }

    public static int fromDip(String dipValue) {
        return (int) (Float.parseFloat(dipValue) * getDensity() + 0.5f);
    }

    public static int fromDip(float dipValue) {
        return (int) (dipValue * getDensity() + 0.5f);
    }


    public static void initCtx(Context context) {
        if (DipUtil.context == null) {
            DipUtil.context = context;
            getDensity();
        }
    }

    /**
     * ��spֵת��Ϊpxֵ����֤���ִ�С����
     *
     * @param spValue
     * @param fontScale ��DisplayMetrics��������scaledDensity��
     * @return
     */
    public static int sp2px(float spValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, Resources.getSystem().getDisplayMetrics());
    }

    /**
     * ��pxֵת��Ϊspֵ����֤���ִ�С����
     *
     * @param pxValue
     * @param fontScale ��DisplayMetrics��������scaledDensity��
     * @return
     */
    public static int px2sp(float pxValue, float fontScale) {
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * Created by avocarrot on 11/12/2014.
     * Every Property of a View is a Dynaic Property
     */


    /**
     * Annotation to create the class ViewHolder
     */


}